import React, { Component } from 'react'
import axios from 'axios';
import PubSub from 'pubsub-js';
export default class Search extends Component {
    render() {
        return (
            <section className="jumbotron">
                <h3 className="jumbotron-heading">Search Github Users</h3>
                <div>
                    <input ref={el => this.input = el} type="text" placeholder="enter the name you search" />&nbsp;
                    <button onClick={this.search}>Search</button>
                </div>
            </section>
        )
    }

    search = async () => {
        //发布消息, 正在请求中...
        PubSub.publish('users', {users:[], isLoading: true})
        //获取 input 输入的内容
        let v = this.input.value;
        //发送 AJAX 请求
        let result = await axios.get('https://api.github.com/search/users', {params: {q: v}});
        //发布数据
        PubSub.publish('users', {users: result.data.items,isLoading: false });
    }
}
